package com.example.useless;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LoginAcitity extends AppCompatActivity {

    private EditText editText1;
    private EditText editText2;
    private EditText editText3;
    private Button mButton;
    private TextView mTestText;
    private ImageView mImage;
    private ActivityResultLauncher mActivityResultLauncher;


    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout.activity_login);
        editText1 = findViewById (R.id.username_edit);
        editText2 = findViewById(R.id.password_edit);
        editText3 = findViewById(R.id.name_edit);
        mButton = findViewById(R.id.submit);
        mImage =findViewById(R.id.photo);


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userName = editText1.getText().toString();
                String password = editText2.getText().toString();
                String name = editText3.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("username",userName);
                intent.putExtra("password",password);
                intent.putExtra("name",name);
                setResult(RESULT_OK,intent);
                onBackPressed();
            }
        });

        mImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//跳转到相机
                mActivityResultLauncher.launch(intent);
            }
        });
    }
    private void onActivityResult() {
        mActivityResultLauncher =
                registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
                        new ActivityResultCallback<ActivityResult>() {
                            @Override
                            public void onActivityResult(ActivityResult result){
                                if (result.getResultCode() == RESULT_OK) {
                                    Bitmap targetBitmap = result.getData().getParcelableExtra("data");//获取数据
                                    mTestText.setBackground(new BitmapDrawable(getResources(),targetBitmap));
                                }
                            }
                });
    }
}
